<?php
/**
 * Created by PhpStorm.
 * User: Kyle
 * Date: 2019/7/17
 * Time: 15:17
 */

namespace app\common\model;


use think\Db;
use think\Exception;
use think\Model;

class UserDistribution extends Model
{
    protected $pk = 'id';

    public function topic(){
        return $this->hasOne('Topic','topic_id','topic_id');
    }

    public function user(){
        return $this->hasOne('User','user_id','user_id');
    }

    public function userFrom(){
        return $this->hasOne('User','user_id','origin_user_id');
    }

    public function secondUser(){
        return $this->hasOne('User','user_id','user_subordinate_id');
    }


    /**
     * 处理用户广告分销的内容
     * @param $userInfo
     * @param $topinInfo
     */
    public function dealAdDistribution($userInfo,$topinInfo){
        $orderDistribution = new UserDistribution();
        $user = new User();
        $insertArray = array();
        if($topinInfo['tt_need_share'] == 2){
            if(!empty($userInfo['fir_distribution'])){
                $distribution['user_id'] = $userInfo['fir_distribution'];
                $distribution['order_id'] = 0;
                $distribution['topic_id'] = $topinInfo['topic_id'];
                $distribution['distribution_percent'] = 0;
                $distribution['origin_price'] = $topinInfo['ad_fir_user'];
                $distribution['get_price'] = $topinInfo['ad_fir_user'];
                $distribution['created_at'] = time();
                $distribution['type'] = 2;
                $distribution['true_price'] = 0;
                $distribution['price_deal_staus'] = 0;
                $distribution['origin_user_id'] = $userInfo['user_id'];
                $distribution['user_type'] = 1;
                $distribution['user_subordinate_id'] = 0;
                $insertArray[] = $distribution;
            }
            if(!empty($userInfo['sec_distribution'])){
                $distribution['user_id'] = $userInfo['sec_distribution'];
                $distribution['order_id'] = 0;
                $distribution['topic_id'] = $topinInfo['topic_id'];
                $distribution['distribution_percent'] = 0;
                $distribution['origin_price'] = $topinInfo['ad_sec_user'];
                $distribution['get_price'] = $topinInfo['ad_sec_user'];
                $distribution['created_at'] = time();
                $distribution['type'] = 2;
                $distribution['true_price'] = 0;
                $distribution['price_deal_staus'] = 0;
                $distribution['origin_user_id'] = $userInfo['user_id'];
                $distribution['user_type'] = 2;
                $distribution['user_subordinate_id'] = $userInfo['fir_distribution'];
                $insertArray[] = $distribution;
            }
            file_put_contents('ad.log','进入广告分销-处理成功'.print_r($userInfo,1)."\r\n",FILE_APPEND);
            file_put_contents('ad.log','进入广告分销-处理成功'.print_r($topinInfo,1)."\r\n",FILE_APPEND);
            file_put_contents('ad.log','进入广告分销-处理成功'.print_r($insertArray,1)."\r\n",FILE_APPEND);
            if($insertArray){
                Db::startTrans();
                try{
                    //查询当前的用户，更新余额
                    $save = $orderDistribution->saveAll($insertArray);

//                    //广告分销不自动结算佣金，需在后台做佣金审核结算
//                    foreach ($insertArray as $v){
//                        $info = $user->get($v['user_id']);
//                        if($info != null){
//                            $info->money = $info->money + $v['get_price'];
//                            $info->total_money = $info->total_money + $v['get_price'];
//                            $info->save();
//                        }
//                    }

                    if(!$save){throw  new Exception('下单存储数据存储失败');}

                    Db::commit();
                }catch (Exception $e){
                    Db::rollback();
                }
            }
        }
    }
}